//
//  MainDiscernSelectWatchTbv.m
//  ZITHER
//
//  Created by weixin on 2018/11/27.
//  Copyright © 2018 weixn. All rights reserved.
//

#import "MainDiscernSelectWatchTbv.h"
#import "MainDiscernTbvCellPic.h"
#import "MainDefaulSearchTypeTwo_V4_Cell.h"


#import "MainSeachBrandVC.h"
#import "MainDefaulSearchVC_V4_Sub.h"
#import "CRBRotateBoxCustomAddVC.h"
#import "CRBRotateBoxCustomBrandVC.h"
#import "CRBRotateBoxCustomChooseManage.h"
#import "CRBRotateBoxCustomSeriesVC.h"


@interface MainDiscernSelectWatchTbv ()



@end

@implementation MainDiscernSelectWatchTbv

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.delegate = self;
        self.dataSource = self;
        self.WatchSeriesIndex = @"";
        self.dicGoodsIndex = @"";
    }
    return self;
}




-(NSMutableArray *)arrM{
    if (!_arrM) {
        _arrM = [[NSMutableArray alloc]init];
        
        letterModel *model1 = [[letterModel alloc]init];
        model1.brandName = @"品牌";
        [_arrM addObject:model1];
        
        MainDiscernSelectWatchSeriesModelSub *model2 = [[MainDiscernSelectWatchSeriesModelSub alloc]init];
        model2.seriesName = @"系列";
        [_arrM addObject:model2];

        goodVOsModel *model3 = [[goodVOsModel alloc]init];
        goodVOsSubModel *mode4 = [[goodVOsSubModel alloc]init];
        mode4.goodName = @"型号";
        model3.goodInfo = mode4;
        
        [_arrM addObject:model3];
  

    }
    return _arrM;
}



#pragma MARK - 数据源
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *str = nil;
    
    if (indexPath.row == 0) {
        return [self getMainDiscernTbvCellPic:indexPath];
    }
//    if (indexPath.row == 1) {
//        letterModel *model1 = self.arrM[0];
//        str = model1.brandName;
//    }else if (indexPath.row == 2){
//        MainDiscernSelectWatchSeriesModelSub *model2 = self.arrM[1];
//        str = model2.seriesName;
//    }else if (indexPath.row == 3){
//            goodVOsModel *model3 =  self.arrM[2];
//        str = model3.goodInfo.goodName;
//    }
    str = [self getstrHeadName:indexPath.row];
    

    if ([str isEqualToString:@"品牌"] ||[str isEqualToString:@"型号"] ||[str isEqualToString:@"系列"] ) {
        return [self getTypeOneCell:str];
    }else{
        return [self getTypeTwoCell:indexPath];
    }
    
}

-(NSString*)getstrHeadName:(NSInteger)index{
    CRBRotateBoxCustomChooseManage *modelManage = [CRBRotateBoxCustomChooseManage shareManager];

    
    if (index == 1) {
        if (modelManage.brandInfoModel.brandNameNative.length == 0) {
            return @"品牌";
        }
        return modelManage.brandInfoModel.brandNameNative;
    }else if (index == 2){
        if (modelManage.customSeriesName.length !=0)
            return modelManage.customSeriesName;
        
        if (modelManage.seriesInfoModel.seriesNameNative.length == 0) {
            return @"系列";
        }
        return modelManage.seriesInfoModel.seriesNameNative;
    }else{
        if (modelManage.customGoodsName.length !=0)
            return modelManage.customGoodsName;
        
        if (modelManage.goodsInfoModel.goodInfo.goodNameNative.length == 0) {
            return @"型号";
        }
        
        return modelManage.goodsInfoModel.goodInfo.goodNameNative;

    }
    
    return @"";
}



-(UITableViewCell *)getMainDiscernTbvCellPic:(NSIndexPath*)indexPath{
    static NSString *sectionCell = @"MainDiscernTbvCellPic";
    MainDiscernTbvCellPic *cell = [self dequeueReusableCellWithIdentifier:sectionCell];
    if (cell == nil) {
        cell = [[MainDiscernTbvCellPic alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell];
    }
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    cell.stare = selectWatch;
    //    cell.model = self.model;
    //    cell.indexPath = index;
    return cell;
    
}

-(UITableViewCell *)getTypeOneCell:(NSString*)str{
    static NSString *sectionCell = @"MainDefaulSearchTypeOne_V4_Cell";
    MainDefaulSearchTypeOne_V4_Cell *cell = [self dequeueReusableCellWithIdentifier:sectionCell];
    if (cell == nil) {
        cell = [[MainDefaulSearchTypeOne_V4_Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell];
    }
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    cell.lab.text = str;
    //    cell.model = self.model;
    //    cell.indexPath = index;
    return cell;
}

-(UITableViewCell *)getTypeTwoCell:(NSIndexPath*)indexPath{
    
    static NSString *sectionCell = @"MainDefaulSearchTypeTwo_V4_Cell";
    MainDefaulSearchTypeTwo_V4_Cell *cell = [self dequeueReusableCellWithIdentifier:sectionCell];
    if (cell == nil) {
        cell = [[MainDefaulSearchTypeTwo_V4_Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell];
    }
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    
    NSString *strHead = nil;
    NSString *str = nil;
    
    NSInteger index = indexPath.row;
    
    if (index == 1 ) {
        letterModel *model1 = self.arrM[0];
        str = model1.brandName;
        strHead = @"品牌";
    }else if (index == 2){
        MainDiscernSelectWatchSeriesModelSub *model2 = self.arrM[1];
        str = model2.seriesName;
        strHead = @"系列";
    }else{
        goodVOsModel *model3 =  self.arrM[2];
        str = model3.goodInfo.goodName;
        strHead = @"型号";
    }
    
    str = [self getstrHeadName:index];

    cell.HeadLab.text = strHead;
    cell.lab.text = str;

    return cell;
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    letterModel *model = self.arrM[0];
    MainDiscernSelectWatchSeriesModelSub *model2 = self.arrM[1];
    goodVOsModel *model3 =  self.arrM[2];
    CRBRotateBoxCustomChooseManage *modelManage = [CRBRotateBoxCustomChooseManage shareManager];

    
    if (indexPath.row == 1) {
        [self pushCRBRotateBoxCustom:0];
//        [self pushMainSeachBrandVC];
    }else if (indexPath.row == 2){
        
        
        if (modelManage.brandInfoModel.brandName.length ==0) {
            [Tools showTips:@"请先选择品牌"];
        }else{
//            [self pushSearchOptionVOsModel:2];
            CRBRotateBoxCustomSeriesVC *vc = [[CRBRotateBoxCustomSeriesVC alloc] init];
            [self.getNav pushViewController:vc animated:YES];
        }
        
    }else if (indexPath.row == 3){
        if (modelManage.seriesInfoModel.seriesName.length ==0) {
            [Tools showTips:@"请先选择系列"];
        }else{
            
            CRBRotateBoxCustomSeriesVC *vc = [[CRBRotateBoxCustomSeriesVC alloc] init];
            [self.getNav pushViewController:vc animated:YES];
//            [self pushSearchOptionVOsModel:3];
        }
        
    }
}


-(void)pushCRBRotateBoxCustom:(NSInteger )index{
    CRBRotateBoxCustomBrandVC *vc = [[CRBRotateBoxCustomBrandVC alloc] init];
    [self.getNav pushViewController:vc animated:YES];
}

-(void)pushMainSeachBrandVC{
    NSLog(@"子条件选择页面");
    MainSeachBrandVC * vc = [[MainSeachBrandVC alloc]init];
    vc.model = self.arrM[0];
    [vc setBlock:^(NSDictionary *dic) {
        letterModel *mdoel2 = [[letterModel alloc]init];
        mdoel2.brandName = dic[@"brandNameNative"];
        mdoel2.id = dic[@"id"];
        self.arrM[0] = mdoel2;
        [self initarrM1];
        [self initarrM2];
        [self reloadData];
        [self loadWatchSeries];
    }];
    [vc setBlockType:^{
        [self initarrM0];
        [self initarrM1];
        [self initarrM2];
        [self reloadData];
        [self loadWatchSeries];
    }];
    [self.getNav pushViewController:vc animated:YES];
}

-(void)pushSearchOptionVOsModel:(NSInteger)index{
    
    MainDefaulSearchVC_V4_Sub *vc = [[MainDefaulSearchVC_V4_Sub alloc]init];
    NSDictionary * dic = @{};
    searchOptionVOsModel *model = [[searchOptionVOsModel alloc]init];

    NSMutableArray *arr = [[NSMutableArray alloc]init];
    
    if (index == 2) {
        MainDiscernSelectWatchSeriesModelSub *model2 = self.arrM[1];
        vc.lab.text = [NSString stringWithFormat:@"搜索条件   >   系列"];
        dic = @{@"title":@"系列",@"num":self.WatchSeriesIndex};
        for (MainDiscernSelectWatchSeriesModelSub *modelSub in self.arrMWatchSeries) {
            optionssModel *opModel = [[optionssModel alloc]init];
            opModel.id = modelSub.id;
            opModel.name = modelSub.seriesName;
            [arr addObject:opModel];
        }
        }else{
        vc.State = STRefreshStateGoods;
        goodVOsModel *model3 =  self.arrM[2];
        vc.lab.text = [NSString stringWithFormat:@"搜索条件   >   型号"];
        dic = @{@"title":@"系列",@"num":self.dicGoodsIndex};
        vc.modelSub = self.arrM[1];
    }
    model.options = arr;
    vc.dic = dic;
    vc.model = model;
    [vc setBlcok:^(NSDictionary * _Nonnull dic) {
    NSString *num = dic[@"num"];
       
        if (index == 2) {
            if (num.length>0) {
                MainDiscernSelectWatchSeriesModelSub *modelSub = self.arrMWatchSeries[[num intValue]];
                self.arrM[index-1] = modelSub;
                self.WatchSeriesIndex = dic[@"num"];
                [self initarrM2];
            }else{
                [self initarrM1];
                [self initarrM2];
                self.WatchSeriesIndex = dic[@"num"];
           }
        }else{
            self.dicGoodsIndex = dic[@"num"];
        }
        
        [self reloadData];
    }];
    
    
    [vc setBlcokGoods:^(NSDictionary * _Nonnull dic, goodVOsModel * _Nonnull model) {
        NSString *num = dic[@"num"];
        if (num.length>0) {
            self.arrM[2] = model;
            self.dicGoodsIndex = dic[@"num"];
        }else{
            [self initarrM2];
            self.dicGoodsIndex = dic[@"num"];
        }
        [self reloadData];
    }];
    
    
    [self.getNav pushViewController:vc animated:YES];
    
}

-(void)loadWatchSeries{
    letterModel *model = self.arrM[0];
    NSString *  str = model.brandName;
    if (![str isEqualToString:@"品牌"]) {
        [QMUITips showLoadingInView:self];
        [[serviceDefualt loadGoodsSeries:model.id reponseObjctClass:[MainDiscernSelectWatchSeriesModel class]]subscribeNext:^(ResponseData* x) {
            [QMUITips hideAllTipsInView:self];
            if (x.success) {
                NSMutableArray *arrM = [[NSMutableArray alloc]init];
                NSArray *arr = x.responseRawData[@"data"];
                for (int i = 0; i<arr.count; i++) {
                    MainDiscernSelectWatchSeriesModelSub *model = [MainDiscernSelectWatchSeriesModelSub yy_modelWithDictionary:arr[i]];
                    [arrM addObject:model];
                }
                self.arrMWatchSeries = arrM;
            }else{
                 [Tools showTips:x.errorInfo];
            }
            
        }];
    }else{
        self.arrMWatchSeries  = [[NSMutableArray alloc]init];
    }
}


-(void)initarrM0{
    letterModel *model1 = [[letterModel alloc]init];
    model1.brandName = @"品牌";
    self.arrM[0] = model1;
}
-(void)initarrM1{
    MainDiscernSelectWatchSeriesModelSub *model2 = [[MainDiscernSelectWatchSeriesModelSub alloc]init];
    model2.seriesName = @"系列";
    self.arrM[1] = model2;
}
-(void)initarrM2{
    goodVOsModel *model3 = [[goodVOsModel alloc]init];
    goodVOsSubModel *mode4 = [[goodVOsSubModel alloc]init];
    mode4.goodName = @"型号";
    model3.goodInfo = mode4;
    self.arrM[2] = model3;
}








-(NSMutableArray *)arrMWatchSeries{
    if (!_arrMWatchSeries) {
        _arrMWatchSeries = [[NSMutableArray alloc]init];
    }
    return _arrMWatchSeries;
}
@end
